<?php
	require("Debug.php");
	if(!empty($_REQUEST)){
		foreach($_REQUEST as $key_request=>$value_request){
			$_REQUEST[$key_request] = str_replace("'",'&prime;',$value_request);
			$_POST[$key_request] = str_replace('"','&prime;',$value_request);
		}
		
	}	
	if(!empty($_POST)){
		foreach($_POST as $key_request=>$value_request){
			$_POST[$key_request] = str_replace("'",'&prime;',$value_request);
			$_POST[$key_request] = str_replace('"','&prime;',$value_request);
		}
		
	}
	ini_set('session.bug_compat_warn', 0);
	ini_set('session.bug_compat_42', 0);

	/**
	 * This action used assign text on image
	 **/
	function watermarkImage($SourceFile, $WaterMarkText,$points = array()) { 

		// load the image from the file specified:
		$im = imagecreatefrompng($SourceFile);
		// if there's an error, stop processing the page:
		if(!$im)
		{
			die("File not found");
		}
		 
		// define some colours to use with the image
		$yellow = imagecolorallocate($im, 255, 255, 0);
		$black = imagecolorallocate($im, 0, 0, 0);
		$w = imagecolorallocate($im, 255,0,0);
		
		 
		// get the width and the height of the image
		$width = imagesx($im);
		$height = imagesy($im);
		 
		// draw a black rectangle across the bottom, say, 20 pixels of the image:
		// imagefilledrectangle($im, 0, ($height-20) , $width, $height, $w);
		 
		// now we want to write in the centre of the rectangle:
		$font = 14;
		$text = $WaterMarkText; // store the text we're going to write in $text
		// calculate the left position of the text:
		$leftTextPos = ( $width - imagefontwidth($font)*strlen($text) )/2;

		// finally, write the string:
		if(empty($points)){
			imagestring($im, $font, $leftTextPos, $height-35, $text, $black);
		}else{
			imagestring($im, $font, $points[0], $points[1], $text, $black);
		}
		 
		// output the image
		// tell the browser what we're sending it
		header('Content-type: image/png');
		// output the image as a png
		imagepng($im);
		 
		// tidy up
		imagedestroy($im);
	}
	/**
	 * This function used call procedure to mysql
	 * Used: $result = callProcedure($sql,true,array('@id_skin','@application','@titre','@ville','@langue'));
	 * Or: $result = callProcedure($sql);
	 */
	
	function callProcedure($arrSql = null,$return = false,$dataReturn = array(),$getMultipleRes = false,$hand = false){

		$is_array  = false;
		$sql = $arrSql;
		$sql0 = null;
		$multipleSQL = false;
		if(is_array($arrSql)){
			$sql = end($arrSql);
			$sql0 = $arrSql[0];
			$multipleSQL = true;
		}
		
		if($return == false){
			
			try{
				$result = mysql_query($sql,connection());
				if (!$result) {
					
					die('Invalid query: ' . mysql_error()." - SQL: {$sql}");
				}else{
					$data = array();
					
					if(@is_resource($result)){
						
					// }else{
					
						$numResults = mysql_num_rows($result);
						if ($numResults > 0) {
							while ($row = mysql_fetch_assoc($result)) {
								
								$data[] = $row;
								//return $desc;
							}
						}
						
						
					}
					return $data;
				}
				
			}catch(Exception $e){
				logsProcedure($sql);
				echo $e->getMessage()."<br>";
				echo $sql."<br>";
				die('Invalid query: ' . mysql_error()." - SQL: {$sql}");
			}
			
		}else{
			
			
			$mydb = connection(1);
			
			
			if(count($arrSql) > 2 && $multipleSQL){
				foreach($arrSql as $k=>$s){
					
					if($k != count($arrSql) - 1):
						//echo $s."<br>";
						$proc = $mydb->prepare($s);
						$proc->execute();
					endif;

				}
			}else{
				if($sql0!=null){
					$proc = $mydb->prepare($sql0);
					$proc->execute();
				}
			}
			
			//echo $sql;
			$proc = $mydb->prepare($sql);
			try{
				$proc->execute();
			}catch(Exception $e){
				logsProcedure($sql);
				echo $e->getMessage()."<br>";
				echo $sql."<br>";
				die('Invalid query: ' . mysql_error()." - SQL: {$sql}");
			}
			
			
			if($hand) return $proc;
			/**
			 * This partial used get multiple result for procedure
			 **/
			if($getMultipleRes){
				$maxRs = 40;
				$data = array();
				$cnt = 0;
				if(!empty($dataReturn)){
					foreach($dataReturn as $key=>$value){
						$row = $proc->fetchAll(PDO::FETCH_ASSOC);
						$data[$value] = $row;
						if($cnt != $maxRs){
							$proc->nextRowset();
						}
						$cnt++;
					}
				}else{
					
				}
				
				$proc->closeCursor();
				
				return $data;
			}else{
			
				if(!empty($dataReturn)){
					$strQuery = "SELECT";
					$cnt = 0;
					foreach($dataReturn as $key=>$data){
						if( count($dataReturn)-1 == $cnt){
							$strQuery.= " {$data}";
						}else{
							$strQuery.= " {$data},";
						}
						$cnt++;
						
					}
					
					$result = $mydb->query($strQuery)->fetch(PDO::FETCH_ASSOC);
					$proc->closeCursor();
					return $result;
				}else{
					$proc->closeCursor();
					return array();
				}
			}
			
		}
	}
	
		
	function otherDiffDate($out_in_array=false,$end=null){
		if($end == null) $end = '2020-06-09 10:30:00';
		
		$end = date("Y-m-d",strtotime($end));
		
        $intervalo = date_diff(date_create(), date_create($end));
        $out = $intervalo->format("Years:%Y,Months:%M,Days:%d,Hours:%H,Minutes:%i,Seconds:%s");
        if(!$out_in_array)
            return $out;
        $a_out = array();
        array_walk(explode(',',$out),
        function($val,$key) use(&$a_out){
            $v=explode(':',$val);
            $a_out[$v[0]] = $v[1];
        });
        return $a_out;
	}
	function dump($obj,$isExit = true) {
		Debug::d($obj,10,true);
		if ($isExit) exit();	
	}
?>